package org.example.studentsystem_back.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import jakarta.annotation.Resource;
import org.example.studentsystem_back.common.CommonResponse;
import org.example.studentsystem_back.domain.Building;
import org.example.studentsystem_back.dto.PageDTO;
import org.example.studentsystem_back.service.BuildingService;
import org.example.studentsystem_back.vo.buildingMangeVo.BuildingVo;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;

@RestController
@Valid
public class BuildingController {
    @Resource
    private BuildingService buildingService;

    @PostMapping("/building")
    public CommonResponse<BuildingVo> getBuilding(@RequestBody String  json) {
        JSONObject jsonObject = JSON.parseObject(json);
        PageDTO pageDTO=jsonObject.toJavaObject(PageDTO.class);
        String id=jsonObject.getString("id");
        return buildingService.findAll(pageDTO,id);
    }

    @PostMapping("/building/add")
    public CommonResponse<Building> addBuilding(@Valid @RequestBody Building building) {
        return buildingService.add(building);
    }

    @PostMapping("/building/update")
    public CommonResponse<Building> updateBuilding(@Valid @RequestBody Building building) {
        return buildingService.update(building);
    }

    @PostMapping("/building/delete")
    public CommonResponse<Building> deleteBuilding(@Valid @RequestBody Building building) {
        return buildingService.delete(building);
    }
}
